from 江苏省数据分析 import *
import time
# print(len(readAllData2()))
def catDatail2(x):
    try:
        f = urllib.request.urlopen(x,timeout=5)
    except:
        print(f"{x}超时")
        return "",""
    else:
        soup = BeautifulSoup(f.read().decode('gbk'),features="html.parser")
        
        tables = soup.select("#page1 table")
        if len(tables) >0:
            detailArr = [] 
            detaildic = {}
            for tr in tables[0].findAll("tr"):
                for td in tr.findAll("td"):
                    detailArr.append(td.getText().strip())
            if len(detailArr) > 0 and detailArr[0]=="企业名称":
                for i in range(0,int(len(detailArr)/2)):
                    detaildic[detailArr[i*2]] = detailArr[i*2+1]
                    
            zizhiArr = []
            for tr in tables[1].select("tr td table tr"):
                tds = []
                for td in tr.findAll("td"):
                    tds.append(td.getText().strip())
                zizhiArr.append(tds)
            return detaildic,zizhiArr
        else:
            print(x)
            return "cuowu",""
def getData3(code,para):
    detail = catDatail2(f"http://58.213.147.230:7001/Jsjzyxyglpt/faces/public/{para}/basicInfoView.jsp?action=viewKcsjqyJbxx&corpCode={code}")
    pers = catLists(f"http://58.213.147.230:7001/Jsjzyxyglpt/faces/public/{para}/content.jsp?action=getZyryList&corpCode={code}")
    projects = catLists(f"http://58.213.147.230:7001/Jsjzyxyglpt/faces/public/{para}/content.jsp?action=getProjectList&corpCode={code}")
    wins = catLists(f"http://58.213.147.230:7001/Jsjzyxyglpt/faces/public/{para}/content.jsp?action=getHjqkList&corpCode={code}")
    losts = catLists(f"http://58.213.147.230:7001/Jsjzyxyglpt/faces/public/{para}/content.jsp?action=getBljlList&corpCode={code}")
#     print(len(detail[0]))
#     print(len(pers))
#     print(len(projects))
#     print(len(wins))
#     print(len(losts))
#     print(f"http://58.213.147.230:7001/Jsjzyxyglpt/faces/public/{para}/content.jsp?action=getZyryList&corpCode={code}")
    if len(detail[0]) > 0 and len(pers)>0 and len(projects)>0 and len(wins)>0 and len(losts)>0:
        return {"detail":detail[0],"cers":detail[1],"pers":pers,"projects":projects,"wins":wins,"losts":losts}
def readAllData3():
    
    def readSpecFile(x):
        with open(x) as f:
            str = f.read()
            j = json.loads(str)
            return j
        
    arr = []
#     for x in readSpecFile('/Users/wangshuguan/江苏省监理企业.txt'):
#         arr.append(x)
#     for x in readSpecFile('/Users/wangshuguan/江苏省勘察企业.txt'):
#         arr.append(x)
    for x in readSpecFile('/Users/wangshuguan/江苏省设计企业.txt'):
        arr.append(x)
    return arr
def saveDatas2(start):
    alldatas = readAllData3()
    for x in range(start,len(alldatas)):
        data = alldatas[x]
        code = data[len(data)-1].split("=")[2]
        def save(filename, contents):
              fh = open(filename, 'w', encoding='utf-8')
              fh.write(contents)
              fh.close()
        result = getData3(code,"kcsjqy")
        
        if result is not None:
            if type(result["detail"]) == type("") and result["detail"] == "cuowu":
                saveDatas2(x+1)
            else:
                save(f'/Users/wangshuguan/江苏省特别企业详情/设计企业/设计企业_{x}_{data[1]}_{data[5]}.txt', json.dumps(result))
                print(x)
                time.sleep(1)
        else:
            saveDatas2(x)
if __name__ == "__main__":
#     print(len(readAllData3()))
    saveDatas2(1170)
    
    
#     d = readAllData3()[111]
#     print(d)
#     code = d[8].split("=")[2]
# #      
#     data = getData3(code,"kcsjqy")
    print(data)